unit engine;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  zgl_main,
  zgl_window,
  zgl_screen,
  zgl_camera_2d,
  zgl_keyboard,
  zgl_mouse,
  zgl_timers,
  zgl_render_target,
  zgl_sprite_2d,
  zgl_textures,
  zgl_primitives_2d,
  zgl_utils,
  zgl_types,
  IniFiles,
  sprite,
  zglSpriteEngine,
  scenemap,
  basescene,
  math;

const
  APP_NAME = 'I Stand Alone [alpha 0.0.1]';

  { TEngine class }

  type
      TEngine = class(TObject)
      private
        function _GetFPS : string;
      public
        constructor CreateEngine;
        destructor FreeEngine;
        property FPS : string read _GetFPS;
      end;

function GetAppDir : string;
function GetHomeDir : string;

procedure ChangeScene(NewScene : TBaseScene);

implementation

var
  scene : TBaseScene;
  chgScene : TBaseScene;
  DirApp : string;
  DirHome : string;

procedure TEngineDraw;
begin

  if (chgScene <> nil) and scene.Running then
  begin
    scene.Dispose;
  end
  else if (chgScene <> nil) and not scene.Running then
  begin
    scene.Free;
    scene := chgScene;
    scene.Init;
    scene.Running := True;
  end;

  if not scene.Freeze then scene.Draw;
end;

procedure TEngineInit;
begin
  scene := TSceneMap.Create;
  scene.Init;
  scene.Running := True;
end;

procedure TEngineUpdate(dt : Double);
begin
   wnd_SetCaption(APP_NAME + ' FPS:' + u_IntToStr(zgl_Get(RENDER_FPS)));
end;

procedure TEngineQuit;
begin

end;

procedure TEngineTimer;
begin
end;

procedure TEngineKeyboard;
begin
  scene.KeyboardInput;
end;

procedure TEngineMouse;
begin
  scene.MouseInput;
end;

function GetAppDir: string;
begin
  Result := DirApp;
end;

function GetHomeDir: string;
begin
  Result := DirHome;
end;

procedure ChangeScene(NewScene: TBaseScene);
begin
  chgScene := NewScene;
end;

function TEngine._GetFPS: string;
begin
  Result := u_IntToStr(zgl_Get(RENDER_FPS));
end;

constructor TEngine.CreateEngine;
var
  IniFile : TIniFile;
  width, height, ID : Integer;
  fullscreen, vsync : Boolean;
begin
  DirApp  := utf8_Copy(PAnsiChar(zgl_Get(DIRECTORY_APPLICATION)));
  DirHome := utf8_Copy(PAnsiChar(zgl_Get(DIRECTORY_HOME)));

  timer_Add(@TEngineTimer, 50);
  timer_Add(@TEngineKeyboard, 10);
  timer_Add(@TEngineMouse, 10);

  zgl_Reg(SYS_LOAD, @TEngineInit);
  zgl_Reg(SYS_DRAW, @TEngineDraw);
  zgl_Reg(SYS_UPDATE, @TEngineUpdate);
  zgl_Reg(SYS_EXIT, @TEngineQuit);

  wnd_SetCaption(APP_NAME);
  wnd_ShowCursor(FALSE);

  // Initialize variables
  width := 800;
  height := 600;
  fullscreen := False;
  vsync := False;

  IniFile := TIniFile.Create(DirApp + DirectorySeparator + 'config.ini');
  try
    width := IniFile.ReadInteger('Main', 'width', 800);
    height := IniFile.ReadInteger('Main', 'height', 600);
    fullscreen := IniFile.ReadBool('Main', 'fullscreen', False);
    vsync := IniFile.ReadBool('Main', 'vsync', False);
  finally
    IniFile.Free;
  end;

  // Set Options
  scr_SetOptions(width, height, REFRESH_MAXIMUM, fullscreen, vsync);

  // Test
  zgl_Init();
end;

destructor TEngine.FreeEngine;
begin
  inherited Destroy;
end;



end.

